package mop.gui.task;

import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.table.TableCellRenderer;
import mop.controller.CreateTaskController;
import mop.controller.ModifyTaskDetailsController;
import mop.controller.RemoveTaskController;
import mop.controller.UpdateTaskStatusController;
import mop.gui.GUI;
import mop.gui.component.JTableButtonModel;
import mop.gui.component.JTableButtonMouseListener;
import mop.gui.component.JTableButtonRenderer;
import mop.model.task.TaskInterface;

/**
 * List of tasks.
 *
 * @author  MOP Team 7
 */
public class TaskPanel extends javax.swing.JPanel
{
    /**
     * Constructor.
     *
     * @param   gui
     * @param   removeProjectController
     */
    public TaskPanel(GUI gui)
    {
        initComponents();
        this.setGui(gui);
    }

    /***************************************************************************
     *                          GUI reference
     ***************************************************************************/

    private GUI gui;

    /**
     * Sets the GUI.
     *
     * @param   gui
     * @post    gui is set.
     */
    private void setGui(GUI gui)
    {
        this.gui = gui;
    }

    /**
     * Returns the GUI reference.
     *
     * @return  this.gui
     */
    public GUI getGui()
    {
        return this.gui;
    }

    /***************************************************************************
     *                             controllers
     ***************************************************************************/

    private CreateTaskController createTaskController;
    private ModifyTaskDetailsController modifyTaskDetailsController;
    private RemoveTaskController removeTaskController;
    private UpdateTaskStatusController updateTaskStatusController;

    /**
     * Adds a controller.
     *
     * @param   createTaskController
     */
    public void addController(CreateTaskController createTaskController) {
        this.createTaskController = createTaskController;
        this.btnCreateTask.setEnabled(true);
    }

    /**
     * Adds a controller.
     *
     * @param   modifyTaskDetailsController
     */
    public void addController(ModifyTaskDetailsController modifyTaskDetailsController) {
        this.modifyTaskDetailsController = modifyTaskDetailsController;
    }

    /**
     * Adds a controller.
     *
     * @param   removeTaskController
     */
    public void addController(RemoveTaskController removeTaskController) {
        this.removeTaskController = removeTaskController;
        tableTasks.addMouseListener(new JTableButtonMouseListener(tableTasks));
    }

    /**
     * Adds a controller.
     *
     * @param   updateTaskStatusController
     */
    public void addController(UpdateTaskStatusController updateTaskStatusController) {
        this.updateTaskStatusController = updateTaskStatusController;
    }

    /***************************************************************************
     *                             tasks table
     ***************************************************************************/

    /**
     * Loads tasks for current user in tasks table.
     */
    public void update()
    {
        if (this.removeTaskController != null) {
            JTableButtonModel tableModel = new JTableButtonModel();

            String[] columns = {"Task Id", "Status", "Details", "Change Status", "Remove"};

            Object[][] rows;

            try {
                LinkedList<TaskInterface> tasks = new LinkedList<TaskInterface>();
                tasks = (LinkedList<TaskInterface>) removeTaskController.getUserTasks();
                // add listeners
                gui.addListeners(tasks);
                rows = generateTaskRows(tasks);
            } catch (NullPointerException e) {
                rows = new Object[1][5];
                rows[0][0] = "";
                rows[0][1] = "";
                rows[0][2] = "";
                rows[0][3] = "";
                rows[0][4] = "";
            }

            tableModel.setColumns(columns);
            tableModel.setRows(rows);

            tableTasks.setModel(tableModel);
            TableCellRenderer defaultRenderer = tableTasks.getDefaultRenderer(JButton.class);
            JTableButtonRenderer buttonRenderer = new JTableButtonRenderer(defaultRenderer);
            tableTasks.setDefaultRenderer(JButton.class, buttonRenderer);
        }
    }

    /**
     * Generates the rows of the tasks table.
     *
     * @param   tasks
     *          List of task descriptions.
     * @return  The rows for the table.
     */
    private Object[][] generateTaskRows(LinkedList<TaskInterface> tasks)
    {
        Object[][] rows = new Object[tasks.size()][5];

        for ( int i = 0 ; i < tasks.size() ; i++ ) {

            rows[i][0] = tasks.get(i).getId();

            rows[i][1] = tasks.get(i).getStatus().getName();

            rows[i][2] = new JButton("Details");
            if (modifyTaskDetailsController != null)
                ((JButton)rows[i][2]).addMouseListener(new TaskDetailsAction(this.getGui(), modifyTaskDetailsController, updateTaskStatusController, tasks.get(i)));
            else
                ((JButton)rows[i][2]).setEnabled(false);

            rows[i][3] = new JButton("Change Status");
            if (updateTaskStatusController != null)
                ((JButton)rows[i][3]).addMouseListener(new TaskStatusAction(this.getGui(), updateTaskStatusController, tasks.get(i)));
            else
                ((JButton)rows[i][3]).setEnabled(false);

            rows[i][4] = new JButton("Remove");
            if (removeTaskController != null)
                ((JButton)rows[i][4]).addMouseListener(new RemoveTaskAction(this.getGui(), removeTaskController, tasks.get(i)));
            else
                ((JButton)rows[i][4]).setEnabled(false);

        }

        return rows;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTasks = new javax.swing.JLabel();
        projectsPane = new javax.swing.JScrollPane();
        tableTasks = new javax.swing.JTable();
        btnCreateTask = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(517, 569));

        lblTasks.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblTasks.setText("Tasks"); // NOI18N

        tableTasks.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        projectsPane.setViewportView(tableTasks);

        btnCreateTask.setText("Create Task"); // NOI18N
        btnCreateTask.setEnabled(false);
        btnCreateTask.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateTaskActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(projectsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblTasks)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 355, Short.MAX_VALUE)
                        .addComponent(btnCreateTask)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCreateTask)
                    .addComponent(lblTasks))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(projectsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCreateTaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateTaskActionPerformed
        (new SelectTaskTypeDialog(gui, createTaskController)).setVisible(true);
}//GEN-LAST:event_btnCreateTaskActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCreateTask;
    private javax.swing.JLabel lblTasks;
    private javax.swing.JScrollPane projectsPane;
    private javax.swing.JTable tableTasks;
    // End of variables declaration//GEN-END:variables

}